﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace Model
{
    /// <summary>
    /// 用以提供在自定义控件中使用的节点类
    /// </summary>
    public class NodeClass
    {
        //界面名称
        string _nodeName;
        //X坐标
        int _x;
        //Y坐标
        int _y;
        //节点颜色
        Color _nodeColor;
        //委托:用以刷新界面
        Action _actionInvalidate;

        int _r, _g, _b;

        /// <summary>
        /// 坐标点
        /// </summary>
        /// <param name="name">坐标名称</param>
        /// <param name="x">X坐标</param>
        /// <param name="y">Y坐标</param>
        /// <param name="nodeColor">颜色</param>
        /// <param name="action">界面刷新</param>
        public NodeClass(string name, int x, int y, Color nodeColor, Action action)
        {
            NodeColor = nodeColor;
            Name = name;
            X = x;
            Y = y;
            _actionInvalidate = action;
        }

        /// <summary>
        /// R通道
        /// </summary>
        public int R
        {
            get
            {
                return _r;
            }
            set
            {
                //判断是否超出范围，在0-255之间
                if (value >= 0 && value <= 255)
                {
                    //赋值
                    _r = value;
                    //重新设置颜色
                    _nodeColor = Color.FromArgb(_r, _g, _b);
                    //刷新界面
                    _actionInvalidate?.Invoke();
                }
                else
                {
                    throw new Exception("R超出范围");
                }
            }
        }

        //G
        public int G
        {
            get
            {
                return _g;
            }
            set
            {
                if (value >= 0 && value <= 255)
                {
                    _g = value;
                    _nodeColor = Color.FromArgb(_r, _g, _b);
                    _actionInvalidate?.Invoke();
                }
                else
                {
                    throw new Exception("G超出范围");
                }
            }
        }

        //B
        public int B
        {
            get
            {
                return _b;
            }
            set
            {
                if (value >= 0 && value <= 255)
                {
                    _b = value;
                    _nodeColor = Color.FromArgb(_r, _g, _b);
                    _actionInvalidate?.Invoke();
                }
                else
                {
                    throw new Exception("B超出范围");
                }
            }
        }

        /// <summary>
        /// 节点颜色
        /// </summary>

        public Color NodeColor
        {
            get
            {
                return _nodeColor;
            }
            set
            {
                _nodeColor = value;
            }
        }

        /// <summary>
        /// 节点名称
        /// </summary>
        public string Name
        {
            get
            {
                return _nodeName;
            }
            set
            {
                _nodeName = value;
                _actionInvalidate?.Invoke();
            }
        }

        public int X
        {
            get
            {
                return _x;
            }
            set
            {
                _x = value;
                _actionInvalidate?.Invoke();
            }
        }

        public int Y
        {
            get
            {
                return _y;
            }
            set
            {
                _y = value;
                _actionInvalidate?.Invoke();
            }
        }

        //重写转字符串的方法
        public override string ToString()
        {
            //字符串拼接的方式: 节点名(X坐标,Y坐标)
            return $"{Name}({X},{Y})";
        }


    }
}
